package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.dao.HttpDao;
import cn.tedu.springmvc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class Http2Controller {
    @Autowired
    HttpDao httpDao;

    @GetMapping("/user")
    public String getUserAll() {
        List<User> list = httpDao.getUserAll();
        return list.toString();
    }

    //localhost:8080/getUserById?id=1  -->  localhost:8080/user/1
    //@PathVariable 表示获取请求路径中参数的值,要求必须同名
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id) {
        User user = httpDao.getUserById(id);
        return user;
    }

    @PostMapping("/user")
    //@RequestBody 从请求体中取值,不加则获取不到值
    public String postUser(@RequestBody User user) {
        httpDao.postUser(user);
        return "添加成功!";
    }

    @PutMapping("/user")
    //@RequestBody 从请求体中取值,不加则获取不到值
    public String putUser(@RequestBody User user) {
        httpDao.putUser(user);
        return "修改成功!";
    }

    @DeleteMapping("user/{id}")
    public String deleteUser(@PathVariable int id) {
        httpDao.deleteUser(id);
        return "删除成功!";
    }
}
